<template>
    
    <!-- 组件的模板 -->
    <div class="page signin-info">
        <div class="calendar bg-color">
            <span class="btn_pre" @click="btn_left"></span>
            <span @click="btn_mid">{{sign_data}}</span>
            <span class="btn_nxt" @click="btn_rignt"></span>
        </div>
        <div class="map">
            <img class="pic" src="../../static/imgs/map.png" alt="" @click="gotoTeamFootPrint">
            <a class="link link_1 touch" href="javascript:;" @click="gotoKaoqinAddMember"><span class="bg_sprite icon"></span>人员范围</a>
            <a class="link link_2 touch" href="javascript:;" @click="gotoTeamFootPrint"><span class="bg_sprite icon"></span>足迹地图</a>
        </div>
        <ul class="mod-tabs">
            <li><a :class="{'current': is_sign_tab}" href="javascript:;" @click="click_sign">已上报({{sign_list_count}})</a></li>
            <li><a :class="{'current': !is_sign_tab}" href="javascript:;" @click="click_not_sign">未上报({{not_sign_list_count}})</a></li>
        </ul>
        <div class="signin-wrap">
            <!-- 已签到 -->
            <empty :show-empty="is_sign_tab && sign_list.length == 0" :msg="'暂无已上报人员'"></empty>
            <ul class="signin-list" v-if="is_sign_tab">
                <li class="com-border-bottom" v-for="item in sign_list">
                    <a class="touch" href="javascript:;">
                        <div class="avatar-box">
                            <img v-if="item.avatar" class="avatar" :src="item.avatar" alt="" @click="gotoProjectMemberDetail(item.user_id,item.group_id)">
                            <img v-else class="avatar" src="../../static/imgs/avatar.png" @click="gotoProjectMemberDetail(item.user_id,item.group_id)">
                            <span class="cover" v-if="item.member_status == -1">已离职</span>
                        </div>
                        <div class="info" @click="gotoSignTjTeamUser(item.user_id,item.group_id)">
                            <h2>{{item.name}}</h2>
                            <p v-if="item.position || item.address"><em>最近上报：</em>{{getLocalTime(item.sign_time*1000)}}（{{item.position || item.address}}）</p>
                            <p v-else><em>最近上报：</em>{{getLocalTime(item.sign_time*1000)}}</p>
                        </div>
                        <span class="count" @click="gotoSignTjTeamUser(item.user_id,item.group_id)">上报<em>{{item.today_sign_count}}</em>次</span>
                    </a>
                </li>
            </ul>
            <!-- 未签到 -->
            <empty :show-empty="!is_sign_tab && not_sign_list.length == 0" :msg="'暂无未上报人员'"></empty>
            <ul class="user-list" v-if="!is_sign_tab">
                <template v-for="item in not_sign_list">
                    <li v-if="item.member_status == 0">
                        <a href="javascript:;" @click="gotoProjectMemberDetail(item.user_id,item.group_id)">
                            
                            <div class="avatar-box">
                                <img class="avatar" v-if="item.avatar" :src="item.avatar" alt="">
                                <img v-else class="avatar" src="../../static/imgs/avatar.png">
                                <!-- <span class="cover" v-if="item.member_status == -1">已离职</span> -->
                            </div>
                            <span class="name">{{item.name}}</span>
                        </a>
                    </li>
                </template>
            </ul>
        </div>
    </div>
</template>

<script>
    let getDataEnd = true   //一次获取数据是否结束

    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    import commonPage from 'clientApi/commonPage.js'
    import widget from 'gapi/widget'
    import clientUi from 'gapi/clientUi'
    import clientInfo from 'gapi/clientInfo.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    import Empty from 'components/Empty/Empty'

    /**
     *  组件实例对象
     */
    export default {
        components: {
            Empty
        },
        data: function(){
            return{
                is_sign_tab: true,
                timestr: 0,
                today: '',
                sign_data: '',
                sign_list_count: 0,
                sign_list: [],
                not_sign_list_count: 0,
                not_sign_list: [],
                user_info_sign: {
                    team_id: 0,
                    project_id: 0,
                    user_id: 0
                },
                userid_list: [],
                page_no: 1,
                page_size: 20,
                key_time: '',//时间戳，记录首次拉取数据的时间  防止中途签到后重复拉取
                showExport: false,
                participantMember_list: [],
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                // alert(location.href)
                //今日时间
                let date = new Date()
                this.timestr = date.getTime()
                this.today = this.sign_data = util.getLocalTime(this.timestr, 'yyyy-MM-dd')

                //获取用户信息
                // cache.getCache('user_info_sign', (data) => {
                    // alert(JSON.stringify(data))
                    // if(data === null){
                        //缓存用户信息
                        this.user_info_sign.team_id = util.getQueryString('team_id')
                        this.user_info_sign.project_id = util.getQueryString('project_id')
                        this.user_info_sign.user_id = util.getQueryString('user_id')
                        cache.setCache('user_info_sign', this.user_info_sign, 100000)
                        cache.setCache('KqCacheMessge', {
                            team_id: this.user_info_sign.team_id,
                            project_id: this.user_info_sign.project_id
                        }, 100000)
                    // }else{
                        // this.user_info_sign = data
                    // }
                    
                    // return
                    


                    //获取当前用户下级所有人员的user_id
                    // commonPage.loadPageStatus('loading')
                    util.ajax({
                        url: '/group/member_id_list',
                        data: {
                            team_id:this.user_info_sign.team_id,
                            project_id: this.user_info_sign.project_id
                        },
                        success: (res) => {
                            console.log('all id')
                            console.log(JSON.stringify(res))
                            // res = JSON.parse(res)
                            // commonPage.loadPageStatus('loadSuccess')
                            if(res&&res.errno==0){
                                this.userid_list = res.data
                                this.participantMember_list = res.data_info.valid_user_id
                                //进入选人页时默认勾选
                                // cache.setMemoryCache('participantMember', this.participantMember_list)

                                //缓存人员列表  预备跳转导出
                                cache.setCache('export_sign_info_cache', {
                                    team_id: this.user_info_sign.team_id,
                                    project_id: this.user_info_sign.project_id,
                                    user_id: this.user_info_sign.user_id,
                                    userid_list: this.userid_list
                                }, 10000)

                                //获取签到人员列表 第一页
                                this.firstGetData()
                                //分页
                                util.scrollPageEnd(this.getMoreData)
                                setTimeout(() => {
                                    //获取未签到人员列表
                                    this.getNotSignList()
                                }, 200)
                                

                            }else if(res.errmsg){
                                 widget.toast(res.errmsg)
                            }else{
                                widget.toast('获取成员列表失败')
                            }
                        },
                        error: (xhr, status) => {
                            // commonPage.loadPageStatus('loadSuccess')
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                          // commonPage.loadPageStatus('loadSuccess')
                          widget.toast('网络连接失败，请检查网络')
                        }
                    })
                // })
                

                //权限信息
                util.ajax({
                    url: '/permission/application',
                    type: 'GET',
                    data: {
                        team_id: this.user_info_sign.team_id,
                        project_id: this.user_info_sign.project_id,
                        application_id: 2
                    },
                    timeout: 10000,
                    success: (data) => {
                        // success
                        console.log(data)
                        if(data.errno == 0){
                            // cache.setCache('permission_info_sign', data.data, 100000)
                            // //判断是否显示导出按钮
                            // data.data.forEach((item) => {
                            //     if(item.id_name == "sign_export"){
                            //         this.showExport = true
                            //     }
                            // })
                            //添加 左右按钮动作
                            this.showRightBtn()
                        }else{
                            widget.toast(data.errmsg)
                        }
                        // commonPage.loadPageStatus('loadSuccess')
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                        // commonPage.loadPageStatus('loadSuccess')
                        // error
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                        // commonPage.loadPageStatus('loadSuccess')
                    }
                })
            },
            firstGetData(){
                this.page_no = 1
                this.sign_list = []
                this.getData()
            },
            getMoreData(){
                
                if(getDataEnd){
                    this.page_no ++
                    this.getData()
                }
                
            },
            getData(){
                // if(this.userid_list.length == 0){
                //     widget.toast('请选择查看人员范围')
                //     return
                // }
                getDataEnd = false
                //获取签到人员列表
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/sign/manager/statistics',
                    type: 'POST',
                    data: {
                        team_id: this.user_info_sign.team_id,
                        project_id: this.user_info_sign.project_id,
                        userid_list: JSON.stringify(this.userid_list),
                        date: this.sign_data,
                        page: this.page_no,
                        page_size: this.page_size,
                        key_time: this.key_time
                    },
                    timeout: 10000,
                    success: (data) => {
                        // success
                        console.log('签到')
                        console.log(data)
                        getDataEnd = true
                        if(data.errno == 0){
                            this.sign_list_count = data.data.sign_count || 0
                            this.sign_list.push(...data.data.list)
                            this.key_time = data.data.key_time
                            if(data.data.list.length < this.page_size){
                                getDataEnd = false
                            }
                        }else{
                            // widget.toast('服务器异常')
                            widget.toast(data.errmsg)
                            // alert(JSON.stringify(data))
                        }
                        // alert(JSON.stringify(data))
                        commonPage.loadPageStatus('loadSuccess')
                    },
                    error: (xhr, status) => {
                        // error
                        getDataEnd = true
                        widget.toast('服务器异常')
                        commonPage.loadPageStatus('loadSuccess')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        getDataEnd = true
                        widget.toast('网络连接失败，请检查网络')
                        commonPage.loadPageStatus('loadSuccess')
                    }
                })
            },
            getNotSignList(){
                //获取未签到人员列表
                // commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/sign/manager/statistics/nosign',
                    type: 'POST',
                    data: {
                        team_id: this.user_info_sign.team_id,
                        project_id: this.user_info_sign.project_id,
                        userid_list: JSON.stringify(this.userid_list),
                        date: this.sign_data
                    },
                    timeout: 10000,
                    success: (data) => {
                        // success
                        // alert(JSON.stringify(data))
                        console.log('未签到')
                        console.log(data)
                        if(data.errno == 0){
                            //包括已删除的数量
                            // this.not_sign_list_count = data.data.count || 0
                            //不包括已删除的数量
                            this.not_sign_list_count = 0
                            data.data.list.forEach((item) => {
                                if(item.member_status == 0){
                                    this.not_sign_list_count ++ 
                                    // console.log(this.not_sign_list_count)
                                }
                            })
                            this.not_sign_list = data.data.list
                        }else{
                            // widget.toast('服务器异常')
                            widget.toast(data.errmsg)
                            // alert(JSON.stringify(data))
                        }
                        // alert(JSON.stringify(data))
                        // commonPage.loadPageStatus('loadSuccess')
                    },
                    error: (xhr, status) => {
                        // error
                        
                        widget.toast('服务器异常')
                        // commonPage.loadPageStatus('loadSuccess')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        
                        widget.toast('网络连接失败，请检查网络')
                        // commonPage.loadPageStatus('loadSuccess')
                    }
                })
            },
            click_sign(){
                //埋点
                let statisticsLog_data = {
                    event_id: '位置上报-已上报',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'haveposition',
                        project_id: this.user_info_sign.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                
                if(!this.is_sign_tab){
                    this.is_sign_tab = true
                }
            },
            click_not_sign(){
                //埋点
                let statisticsLog_data = {
                    event_id: '位置上报-未上报',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'noposition',
                        project_id: this.user_info_sign.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                
                if(this.is_sign_tab){
                    this.is_sign_tab = false
                }

            },
            btn_left (){
                
                
                clientInfo.getNetworkType((data) => {
                    // alert(JSON.stringify(data))    // 网络信息
                    if(data.network == 'invalid'){
                        widget.toast("网络连接失败，请检查网络")
                        return
                    }
                    //埋点
                    let statisticsLog_data = {
                        event_id: '位置上报-左箭头',
                        // event_label: data.event_label,(预留，现在不用传此参数)
                        event_param: {
                            ca_from: 'predate',
                            project_id: this.user_info_sign.project_id,
                        }
                    }
                    // alert(JSON.stringify(statisticsLog_data))
                    KCApiApp.statisticsLog(statisticsLog_data)
                    // alert(typeof this.timestr)
                    this.timestr = this.timestr - 3600 * 24 * 1000
                    this.sign_data = util.getLocalTime(this.timestr, 'yyyy-MM-dd')

                    getDataEnd = true
                    this.firstGetData()
                    this.getNotSignList()
                })

                
            },
            btn_rignt (){
                if(this.today > this.sign_data){
                    

                    clientInfo.getNetworkType((data) => {
                        // alert(JSON.stringify(data))    // 网络信息
                        if(data.network == 'invalid'){
                            widget.toast("网络连接失败，请检查网络")
                            return
                        }
                        //埋点
                        let statisticsLog_data = {
                            event_id: '位置上报-右箭头',
                            // event_label: data.event_label,(预留，现在不用传此参数)
                            event_param: {
                                ca_from: 'nextdate',
                                project_id: this.user_info_sign.project_id,
                            }
                        }
                        // alert(JSON.stringify(statisticsLog_data))
                        KCApiApp.statisticsLog(statisticsLog_data)
                        this.timestr = this.timestr + 3600 * 24 * 1000
                        this.sign_data = util.getLocalTime(this.timestr, 'yyyy-MM-dd')

                        getDataEnd = true
                        this.firstGetData()
                        this.getNotSignList()
                    })
                }else{
                    widget.toast('不能查看当日以后的日期')
                }
                
            },
            btn_mid (){
                KCApiApp.showDatePicker('2000-01-01', this.today, this.sign_data, (data) => {
                    

                    clientInfo.getNetworkType((networkData) => {
                        // alert(JSON.stringify(networkData))    // 网络信息
                        if(networkData.network == 'invalid'){
                            widget.toast("网络连接失败，请检查网络")
                            return
                        }
                        //埋点
                        let statisticsLog_data = {
                            event_id: '位置上报-顶部日期',
                            // event_label: data.event_label,(预留，现在不用传此参数)
                            event_param: {
                                ca_from: 'date',
                                project_id: this.user_info_sign.project_id,
                            }
                        }
                        // alert(JSON.stringify(statisticsLog_data))
                        KCApiApp.statisticsLog(statisticsLog_data)

                        this.sign_data = data.SelectedDate
                        this.timestr = util.getStampFromDate(data.SelectedDate)

                        getDataEnd = true
                        this.firstGetData()
                        this.getNotSignList()
                    })
                })
            },
            gotoExportSign(){//native 实现  缓存需要带的数据
                // if(this.sign_list.length > 0){
                    let export_sign_info = {
                        team_id: this.user_info_sign.team_id,
                        project_id: this.user_info_sign.project_id,
                        user_id: this.user_info_sign.user_id,
                        userid_list: this.userid_list
                    }
                    let temp = JSON.stringify(export_sign_info)
                    let encodeStr = encodeURIComponent(`export_sign_info=${temp}&origin_page=sign`)
                    location.href = `dmsaas://ExportSign?urdData=${encodeStr}`
                // }else{
                //     clientUi.alertDialog('提醒', '当前选择日期内无提交记录,请选择其他日期', () => {
                        
                //     })
                    
                // }
                
            },
            gotoKaoqinAddMember(){
                clientInfo.getNetworkType((data) => {
                    if(data.network == 'invalid'){
                        widget.toast("网络连接失败，请检查网络")
                        return
                    }
                    //埋点
                    let statisticsLog_data = {
                        event_id: '位置上报-人员范围',
                        // event_label: data.event_label,(预留，现在不用传此参数)
                        event_param: {
                            ca_from: 'memberarea',
                            project_id: this.user_info_sign.project_id,
                        }
                    }
                    KCApiApp.statisticsLog(statisticsLog_data)
                    cache.setMemoryCache('participantMember', [])
                    let encodeStr = encodeURIComponent(`origin_type=sign`)
                    location.href = `dmsaas://KaoqinAddMember?urdData=${encodeStr}`
                })
            },
            gotoTeamFootPrint(){
                clientInfo.getNetworkType((data) => {
                    if(data.network == 'invalid'){
                        widget.toast("网络连接失败，请检查网络")
                        return
                    }
                    //埋点
                    let statisticsLog_data = {
                        event_id: '位置上报-足迹地图or直接点击地图',
                        // event_label: data.event_label,(预留，现在不用传此参数)
                        event_param: {
                            ca_from: 'foorprint',
                            project_id: this.user_info_sign.project_id,
                        }
                    }
                    // alert(JSON.stringify(statisticsLog_data))
                    KCApiApp.statisticsLog(statisticsLog_data)

                    // if(this.sign_list.length == 0){
                    //     widget.toast('当日无已签到人员')
                    //     return
                    // }
                    let tempObj = {
                        team_id: this.user_info_sign.team_id,
                        project_id: this.user_info_sign.project_id,
                        userid_list: this.userid_list,
                        date: this.sign_data
                    }
                    let tempJson = JSON.stringify(tempObj)
                    let encodeStr = encodeURIComponent(`team_sign_info=${tempJson}`)
                    location.href = `dmsaas://TeamFootPrint?urdData=${encodeStr}`
                })
            },
            gotoSignTjTeamUser(sign_userid,group_id){
                //埋点
                let statisticsLog_data = {
                    event_id: '位置上报-查看人员上报详情',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'member',
                        project_id: this.user_info_sign.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                
                let tempObj = {
                    team_id: this.user_info_sign.team_id,
                    project_id: this.user_info_sign.project_id,
                    sign_userid: sign_userid,
                    timestr: this.timestr,
                    group_id: group_id
                }
                let tempJson = JSON.stringify(tempObj)
                let encodeStr = encodeURIComponent(`sign_info=${tempJson}`)
                location.href = `dmsaas://SignTjTeamUser?urdData=${encodeStr}`

            },
            gotoProjectMemberDetail(user_id,group_id){
                let encodeStr = encodeURIComponent(`tid=${this.user_info_sign.team_id}&pid=${this.user_info_sign.project_id}&gid=${group_id}&uid=${user_id}`)
                location.href = `dmsaas://ProjectMemberDetail?urdData=${encodeStr}`
            },
            getLocalTime(timestr){
                return util.getLocalTime(timestr, 'HH:mm')
            },
            showRightBtn(){
                commonPage.updateTitleBar({
                    backAction: 'delselect',
                    // leftDisplay: false,
                    // leftText: '',
                    leftAction: 'delselect',
                    // rightDisplay: this.showExport,
                    // rightText: '导出上报记录',
                    // rightAction: 'doExport'
                })
            }
            
        },
        created(){

            this.init();
            
            window.doExport = this.gotoExportSign
            window.loadData = (data) => {
                // alert(JSON.stringify(data))
                if(data.key === 'participantMember'){
                    this.participantMember_list = data.value
                    if(data.value.length > 0){
                        this.userid_list = data.value
                        //签到人员
                        this.firstGetData()
                        //未签到
                        this.getNotSignList()
                    }else if(data.value.length == 0){
                        this.userid_list = []
                        this.sign_list_count = 0
                        this.firstGetData()
                    }else{
                        this.userid_list = []
                    }
                    //进入人员选择页默认勾选
                    // cache.setMemoryCache('participantMember', data.value)

                    cache.setCache('export_sign_info_cache', {
                        team_id: this.user_info_sign.team_id,
                        project_id: this.user_info_sign.project_id,
                        user_id: this.user_info_sign.user_id,
                        userid_list: this.userid_list
                    }, 10000)
                }
            }
            window.delselect = () => {
                cache.setMemoryCache('participantMember', [])
                commonPage.goBack(1)
            }
            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('webViewID',{'webViewID':data})
                // window.location.href="dmsaas://KaoqinAddGroup"
            })
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<!-- <style src="../../static/css/user-list.css"></style> -->
<style src="../../static/css/attendance.css"></style>
<style type="text/css">
    div.mod-empty{
        margin-top: 40px;
    }
    div.page .mod-empty .tip{
        color:#bbb;
    }
</style>


